// pages/index/appraise-detail/detail.js

const requestUtil = require("../../../utils/request");
const api = require("../../../config/api");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    date: null, // 选择的时间
    formObj: null, // 表单的对象
    formData: null, // 提交的数据
    imgList: [], // 照片列表
    imgMaxLength: 4, // 照片最多张数（最大9）
    dutyType: 0, // 0无权限，1组员，2组长，3值周行政
    tabType: 0, // 0未审批，1已审批
    scoreType: 'add', // add加分，sub扣分
    rejectReason: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let dutyType = options.dutyType;
    let tabType = options.tabType;
    let userInfo = app.globalData.teacherInfo[0];
    this.setData({
      dutyType,
      tabType,
      userInfo,
    })
    this.getRegDetail(id);
  },

  /**
   * 获取打分详情
   */
  async getRegDetail(id) {
    let res = await requestUtil.request("GET", api.regDetail, {
      id: id
    });
    if (res.data.code != 0) {
      wx.showToast({
        title: '获取信息失败，请联系管理员。',
        icon: 'none'
      })
      return;
    }
    let data = res.data.data;
    let obj = {
      weekNo: data.weekNo,
      marker: data.regTeacherName,
      plateType: data.plateType,
      clazz: data.gradeName + data.clazz + '班',
      date: data.createTime.substr(0, 10),
      score: data.score == null ? data.score : Number(data.score) > 0 ? data.score : data.score.toString().substr(1),
      reason: data.reason,
      status: data.status,
      auditTeacherName: data.auditTercherName,
      auditReason: data.auditReason
    }
    let formData = {
      schoolId: data.schoolId,
      confId: data.confId,
      clazz: data.clazz,
      gradeName: data.gradeName,
      plateType: data.plateType,
    }
    let imgList = [];
    for (let i of data.attachList) {
      let url = api.picUrl + i.attachUrl;
      imgList.push(url);
    }
    this.setData({
      formObj: obj,
      formData: formData,
      id: id,
      imgList: imgList,
      scoreType: Number(data.score) > 0 ? 'add' : 'sub'
    })
  },

  /**
   * 选择日期触发
   */
  DateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  /**
   * 预览图片
   */
  ViewImage(e) {
    app.globalData.loadShow = false; // 先禁用chooseImage和uploadFile导致调用app.onShow
    let imgList = this.data.imgList;
    wx.previewImage({
      urls: imgList,
      current: e.currentTarget.dataset.url
    });
  },

  /**
   * 表单提交
   */
  formSubmit(e) {
    let formData = e.detail.value;
    //TODO 然后把日期放到formData里即可，其他数据都有了
  },

  /**
   * 加扣分单选框变更
   */
  ChangeScoreRadio(e) {
    this.setData({
      scoreType: e.detail.value
    })
  },

  /**
   * 隐藏确认审批的modal
   */
  hideAgreeModal(e) {
    this.setData({
      modalName: null,
      scoreType: 'add'
    })
  },

  /**
   * 同意评比
   */
  agreeAppraisal: function (e) {
    let scoreType = this.data.scoreType;
    let score = Number(e.detail.value.score);
    if(score == 0) {
      wx.showToast({
        title: '分数不能为0',
        icon: 'none'
      });
      return;
    }
    if (scoreType == 'sub') {
      score = (score < 0 ? score : 0 - score);
    }else {
      score = (score < 0 ? 0 - score : score);
    }
    this.submitAgree(score);
  },

  /**
   * 确认提交评比
   */
  submitAgree: async function (score) {
    let _this = this;
    let teacherId = this.data.userInfo.id;
    let res = await requestUtil.request("POST", api.regAudit, {
      id: this.data.id,
      auditTeacherId: teacherId,
      status: 1,
      score: score
    })
    if (res.data.code == 0) {
      _this.hideAgreeModal();
      wx.showToast({
        title: '确认成功',
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        })
      }, 2000)
    } else {
      wx.showToast({
        title: '失败，' + res.data.msg,
        icon: 'none'
      })
      console.error("提交打分失败，原因为：", res.data.msg)
    }
  },

  /**
   * 取消打分
   */
  submitCancel() {
    let _this = this;
    wx.showModal({
      title: '确认',
      content: '确认取消吗？',
      success: (res) => {
        if (res.confirm) {
          _this.submitCancel(e.currentTarget.dataset.id);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 确认驳回
   */
  async submitReject() {
    let _this = this;
    let reason = _this.data.rejectReason;
    if(!reason) {
      wx.showToast({title: '请输入驳回原因',icon: 'none'});
      return;
    }
    let res = await requestUtil.request("POST", api.regAudit, {
      id: this.data.id,
      auditTeacherId: this.data.userInfo.id,
      status: 2,
      auditReason: reason
    })
    if (res.data.code == 0) {
      wx.showToast({
        title: '已驳回',
      })
      _this.setData({
        modalName: null
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        })
      }, 2000)
    } else {
      wx.showToast({
        title: '驳回失败，' + res.data.msg,
        icon: 'none'
      })
      console.error("提交打分失败，原因为：", res.data.msg)
    }
  },

  /**
   * 显示Modal
   */
  showModal(e) {
    let modalName = e.currentTarget.dataset.target;
    this.setData({
      modalName: modalName,
      agreeAppraisalScore: this.data.formObj.score
    })
  },

  /**
   * 隐藏modal
   */
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  /**
   * 隐藏驳回的modal
   */
  hideRejectModal(e) {
    this.setData({
      modalName: null,
      rejectId: null,
      rejectReason: null
    })
  },

  /**
   * 监听驳回弹出窗的textarea输入
   */
  rejectTextareaInput(e) {
    this.setData({
      rejectReason: e.detail.value
    })
  },
})